import { trans } from '@utils/common'
import { post, get, postL } from '@utils/http'
import { SERVE_URL } from '@utils/config'
import { cache } from '@utils'
import store, { STORE_KEY } from '@store'

export default {
    apis: {
        //==========B端用户========
        // CODE: SERVE_URL + 'apiExp/manager/loginUtil/image',
        CODE: SERVE_URL + 'apiExp/manager/captcha',
        LOGIN: SERVE_URL + 'apiExp/manager/login',
        LOGINOUT: SERVE_URL + 'apiExp/manager/logout',
        INFO: SERVE_URL + 'apiExp/manager/getUserPermissionInfo',
        UPDATE_PWD: SERVE_URL + 'apiExp/manager/sysUser/editPwd',
        ROUTERS: SERVE_URL + 'apiExp/manager/sysMenu/menuTree',
    },
    // 获取验证码
    async codeImg() {
        const res = await trans(
            get(this.apis.CODE, null, {
                headParam: { isToken: false },
            })
        );
        return res.resultCode == 8200 ? res.data : false;
    },
    async login(param, userName, password, captcha, picKey) {
        const res = await trans(
            post(
                this.apis.LOGIN,
                {
                    ...param,

                    // userName,
                    // password,
                    // captcha,
                    // picKey
                },
                { headParam: { isToken: false } }
            )
        );
        // if (res.data) {
        //     store.commit(STORE_KEY.M_UPDATE_USER, res.data.userVo);
        // }
        return res;
    },
    // 获取用户详细信息
    async info() {
        const res = await trans(post(this.apis.INFO));
        if (res.success) {
            store.commit(STORE_KEY.M_UPDATE_USER, res.data.userInfo);
            //TODO: 后期删除
            store.commit(STORE_KEY.M_UPDATE_USER, res.data);

            // const permissions = res.data.permissions || [];
            // store.commit(STORE_KEY.M_UPDATE_PREMISSION, permissions);
        }
        return res.success ? res : false;
    },

    async updatePassword(oldPwd, newPwd, confirmNewPwd) {
        const res = await trans(postL(this.apis.UPDATE_PWD, { newPwd, oldPwd, confirmNewPwd }));
        return res.success;
    },
    async loginOut() {
        const res = await trans(get(this.apis.LOGINOUT));
        return res;
    },

    async routers() {
        const user = store.getters.user;
        const permissions = store.getters.permissions;
        const res = await trans(post(this.apis.ROUTERS, { userId: user.id, perm: permissions }));
        if (res.success && res.data) {
            res.data.forEach(item => {
                // item.name = item.path.replace('/', '');
                if (item.children) {
                    item.children.forEach(item1 => {
                        item1.path = item1.path.replace(item.path, '');
                        if (item1.path[0] === '/') {
                            item1.path = item1.path.substr(1);
                        }
                    });
                }
            });
            cache.set(cache.key.ROUTERS, res.data);
        }
        return res;
    },
};
